﻿using System;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using BoardPACUtil.Util;


namespace BoardPACUtil.Settings
{
    public static class Settings
    {
        private const char DELIMITER = ',';

        private const string EMAIL_CLIENT = "EmailClient";
        private const string EMAIL_PORT = "EmailPort";
        private const string EMAIL_USER_NAME = "EmailUserName";
        private const string EMAIL_PASSWORD = "EmailPassword";

        private const string CATEGORY_SUBCATEGORY_MANIPULATION_ENABLE = "Category_SubCategory_Manipulation_Enable";
        private const string IMAGE_GALLERY_PATH = "ImageGalleryPath";

        private const string MEETING_DECISION_STATUS = "MeetingDecisionStatus";
        private const string CIRCULAR_DECISION_STATUS = "CircularDecisionStatus";
        private const string NEED_PAPERREF_NUMBER = "NeedPaperRefNumber";

        private const string AGENDA_TEMPLATE_PATH = "AgendaTemplatePath";
        private const string ENABLE_TEMPLATE_SHARING = "EnableTemplateSharing";

        private const string ENCRYPTION_TYPE = "EncryptionType";

        private const string LICENCE_TEXT_PATH = "licenseText";
        private const string LICENCE_LIC_PATH = "licenseLic";

        //Email subjects
        private const string RESET_PASSWORD_BY_ADMIN = "BoardPAC Password Reset Notification";


        /// Author: Suren Manawatta.
        /// Date: 2012-06-13.
        /// <summary>
        /// The function used to access Web.Config file and get config information to
        /// create a SmtpClient with or without SSL
        /// </summary>
        /// <param name="sslOn">Use 'True' for SSL on and, 'False' for SSL off</param>
        /// <returns></returns>
        public static SmtpClient GetEmailConfigs(bool sslOn)
        {
            SmtpClient client = new SmtpClient(GetAppSettingsString(EMAIL_CLIENT), GetAppSettingsInt(EMAIL_PORT))
               {
                   Credentials = new NetworkCredential(GetAppSettingsString(EMAIL_USER_NAME), GetAppSettingsString(EMAIL_PASSWORD)),
                   EnableSsl = sslOn
               };

            return client;
        }

        /// Author: Suren Manawatta.
        /// Date: 2012-07-04
        /// <summary>
        /// Use to check the feature is on or off
        /// </summary>
        /// <returns>bool value. true for feature on and false for feature off</returns>
        public static bool CheckAuth()
        {
            return GetAppSettingsBool(CATEGORY_SUBCATEGORY_MANIPULATION_ENABLE);
        }


        /// <summary>
        /// Gets the decision statuses based on the meeting type
        /// </summary>
        /// <param name="meetingType">meeting type</param>
        /// <returns>string array status names</returns>
        public static string[] GetDecisionStatuses(int meetingType)
        {
            if (meetingType == ApplicationConstants.MEETING)
            {
                return GetAppSettingsStringArray(MEETING_DECISION_STATUS);
            }
            else if (meetingType == ApplicationConstants.CIRCULAR)
            {
                return GetAppSettingsStringArray(CIRCULAR_DECISION_STATUS);
            }

            return ApplicationConstants.EMPTY_STRING_ARRAY;
        }

        /// <summary>
        /// 
        /// </summary>
        public static string ResetPasswordEmailSubject
        {
            get { return RESET_PASSWORD_BY_ADMIN; }
        }

        /// <summary>
        /// Gets the virtual path where the agenda template file gets saved
        /// </summary>
        public static string AgendaTemplatePath
        {
            get { return GetAppSettingsString(AGENDA_TEMPLATE_PATH); }
        }

        /// <summary>
        /// Gets the template sharing status
        /// </summary>
        public static bool IsTemplateSharingEnabled
        {
            get { return GetAppSettingsBool(ENABLE_TEMPLATE_SHARING); }
        }

        /// <summary>
        /// Gets the virtual path where the Image Galley is saved
        /// </summary>
        public static string ImageGalleryPath
        {
            get { return GetAppSettingsString(IMAGE_GALLERY_PATH); }
        }

        /// Author: Thusyanthan A.
        /// Date: 2013-01-28
        /// <summary>
        /// To get the settings of whether paper ref needed
        /// </summary>
        /// <returns>bool value. true for feature on and false for feature off</returns>
        public static bool CheckPaperRefNeeded()
        {
            return GetAppSettingsBool(NEED_PAPERREF_NUMBER);
        }


        /// Author: Thusyanthan A.
        /// Date: 2013-04-23
        /// <summary>
        /// To get the configuration which algorithm to use to encrypt the files
        /// </summary>
        /// <returns>string value. name of the algorithm</returns>
        public static string GetEncryptionType()
        {
            return GetAppSettingsString(ENCRYPTION_TYPE);
        }

        public static string LicenseTextPath
        {
            get { return GetAppSettingsString(LICENCE_TEXT_PATH); }
        }

        public static string LicenseLicPath
        {
            get { return GetAppSettingsString(LICENCE_LIC_PATH); }
        }

        #region Util

        /// <summary>
        /// Common method to get String param values
        /// </summary>
        /// <param name="paramName">app setting parameter name</param>
        /// <returns></returns>
        private static string GetAppSettingsString(string paramName)
        {
            if (ConfigurationManager.AppSettings[paramName] == null)
                throw new ApplicationException(string.Format("{0} is not defined in config file", paramName));

            return ConfigurationManager.AppSettings[paramName];
        }

        /// <summary>
        /// Common method to get int param values
        /// </summary>
        /// <param name="paramName">app setting parameter name</param>
        /// <returns>parameter value as int</returns>
        private static int GetAppSettingsInt(string paramName)
        {
            return int.Parse(GetAppSettingsString(paramName));
        }

        /// <summary>
        /// Common method to get bool param values
        /// </summary>
        /// <param name="paramName">app setting parameter name</param>
        /// <returns>parameter value as boolean</returns>
        private static bool GetAppSettingsBool(string paramName)
        {
            bool temp = false;
            return bool.TryParse(GetAppSettingsString(paramName), out temp) ? temp : false;
        }

        /// <summary>
        /// Common method to get comma seperated string param values as an array of string elements
        /// </summary>
        /// <param name="paramName">app setting parameter name</param>
        /// <returns>string array</returns>
        private static string[] GetAppSettingsStringArray(string paramName)
        {
            string encoded = GetAppSettingsString(paramName);
            return encoded.Contains(DELIMITER.ToString()) ? encoded.Split(DELIMITER) : new string[] { encoded };
        }

        #endregion
    }
}
